// Main function for shortening a link
function shortenLink() {
    // Get the currently selected tab
    chrome.tabs.query({active:true, currentWindow:true}, function (tabs) {
        // Function for processing the response from the goo.gl API
        function processResponse() {
            // Get the shortened URL from the response
			resp = JSON.parse(request.responseText);
			if (resp.status != 0){
				shortUrl = resp.status_txt;
			} else {
				shortUrl = resp.tinyurl;
			}
 
            // Make sure the URL was shortened successfully
            if (shortUrl != undefined) {
                // Update the short URL text on the page
                shortUrlBox.value = shortUrl;
                
                // Copy the link of the short URL if requested
                if (copyLink) {
                        shortUrlBox.select();
                        document.execCommand('copy', false, null);
                        shortUrlBox.selectionEnd = 0;
                        subText.innerText = 'Copied link to clipboard';
                        subText.style.display = 'inline';
                }
                
                // Display a QR code of the shortened URL if requested
                //if (showQr) {
				if (false) {
                    qrCodeImg.src = 'https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=' + encodeURI(shortUrl) + '&choe=UTF-8';
                    qrCode.style.display = 'inline';
                    qrCodeBox.value = 'https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=' + encodeURI(shortUrl) + '&choe=UTF-8';
                }
            }
        };
        
        // Get the URL and data to send with the request
        //data = '{"url": "' + tabs[0].url + '"}';
		data = "url=" + tabs[0].url;
        if (bgPage.oauth.hasToken()) {
			//url = 'http://goto.baidu.com/api/add';
			//url = 'http://cq01-rdqa-pool212.cq01.baidu.com:8080/api/add';
			url = 'http://dwz.cn/create.php';
            //url = bgPage.oauth.signURL('https://www.googleapis.com/urlshortener/v1/url', 'POST', { 'key' : 'AIzaSyBotdU3lv8mi2oA1FaWhOzVelQ6VKChVno' });
        } else {
			url = 'http://cq01-rdqa-pool212.cq01.baidu.com:8080/api/add';
			//url = 'http://goto.baidu.com';
            //url = 'http://dwz.cn/create.php'; //https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyBotdU3lv8mi2oA1FaWhOzVelQ6VKChVno';
		}

        // Create the request and send the data
        request = new XMLHttpRequest();
        request.open('POST', url, false);
        //request.setRequestHeader('Content-Type', 'application/json');
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        request.onload = processResponse;
        request.send(data);
    });
}

// Function for copying the link of the QR code
function copyQrLink() {
    if (subText.innerText != 'Copied QR to clipboard') {
        qrCodeBox.style.display = 'inline';
        qrCodeBox.select();
        document.execCommand('copy', false, null);
        qrCodeBox.selectionEnd = 0;
        qrCodeBox.style.display = 'none';
        subText.innerText = 'Copied QR to clipboard';
        subText.style.display = 'inline';
    }
    else if (copyLink) {
        shortUrlBox.select();
        document.execCommand('copy', false, null);
        shortUrlBox.selectionEnd = 0;
        subText.innerText = 'Copied link to clipboard';
    }
}

// Initialize the popup page
document.addEventListener('DOMContentLoaded', function () {
    // Set various variables used in this page
    shortUrlBox = document.getElementById('shortUrlBox');
    subText = document.getElementById('subText');
    qrCode = document.getElementById('qrCode');
    qrCodeImg = document.getElementById('qrCodeImg');
    qrCodeBox = document.getElementById('qrCodeBox');
    bgPage = chrome.extension.getBackgroundPage();
    document.getElementById('qrCodeImg').addEventListener('click', copyQrLink);
    
    // Load the user's configuration and start shortening the link
    loadCfg();
    shortenLink();
});
